package com.kurumi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.kurumi.bo.Page;
import com.kurumi.bo.PageData;
import com.kurumi.bo.PageParam;
import com.kurumi.config.Constant;
import com.kurumi.pojo.Users;
import com.kurumi.service.DicService;
import com.kurumi.service.UserService;
import com.kurumi.util.JsonUtil;

@Controller
@RequestMapping("/users")
public class UsersController {

	@Autowired
	private UserService userService;

	@Autowired
	private DicService dicService;

	/**
	 * 跳转到用户信息页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toUserListPage")
	public ModelAndView toUserListPage() throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		return new ModelAndView("/users/userListPage", model);
	}

	/**
	 * 查询用户信息
	 * 
	 * @param request
	 * @param response
	 * @param pageParam
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getUserList")
	public void getUserList(HttpServletRequest request, HttpServletResponse response, PageParam pageParam)
			throws Exception {
		Page pager = new Page();
		pager.setPage(pageParam.getPage());
		pager.setRecPerPage(pageParam.getLimit());

		PageData<Map<String,Object>> pd = new PageData<Map<String,Object>>();
		PageInfo<Map<String,Object>> pageInfo = userService.getUsersPageInfo(request, pageParam);

		List<Map<String,Object>> list = pageInfo.getList();

		if (list != null && list.size() > 0) {
			pd.setCount((int) pageInfo.getTotal());
			pd.setData(list);
		} else {
			pd.setCount(0);
			pd.setData(new ArrayList<Map<String,Object>>());
		}

		String json = JsonUtil.objectToJson(pd);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 跳转至微信用户添加页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userAddPage")
	public ModelAndView userAddPage(Integer userId) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();

		Users users = userService.getUserById(userId);
		model.put("user", users);
		model.put("view", 1);
		// 胰岛素注射剂量每日
		model.put("dic_injectinsulin_times", dicService.getDicList("dic_injectinsulin_times"));

		return new ModelAndView("/users/userAddPage", model);
	}

	/**
	 * 删除微信用户信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/delUserById")
	public void delUserById(HttpServletRequest request, HttpServletResponse response, Integer id) throws Exception {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;

		JSONObject jsonObject = new JSONObject();

		try {

			userService.deleteUserById(id);

			jsonObject.put("code", "1");
			jsonObject.put("msg", "操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("code", "0");
			jsonObject.put("msg", "操作失败");
		} finally {
			out = response.getWriter();
			out.print(jsonObject.toString());
			out.flush();
			out.close();
		}
	}

}
